package com.fr.plugin.utils;

import com.fr.decision.webservice.Response;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import com.fr.web.utils.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class ResponseUtils {
    private static final int SUCCESS = 200;
    private static final int FAILED = -1;

    public static void successResponse(HttpServletResponse res, String body) {
        response(res, body, SUCCESS);
    }

    public static void failedResponse(HttpServletResponse res, String body) {
        response(res, body, FAILED);
    }

    private static void response(HttpServletResponse res, String body, int code) {

        FRUtils.FRLogInfo("body:"+body+";code:"+code);
        JSONObject object = new JSONObject();
        PrintWriter pw;
        try {
            object.put("code", code);
            object.put("data", body);
            pw = WebUtils.createPrintWriter(res);
        } catch (Exception e) {
            FineLoggerFactory.getLogger().info(e.getMessage());
            return;
        }
        res.setContentType("application/json;charset=utf-8");
        String result = object.toString();
        pw.println(result);
        pw.flush();
        pw.close();
    }

    public static void response(HttpServletResponse res,JSONObject json){
        PrintWriter pw;
        try {
            pw = WebUtils.createPrintWriter(res);
        } catch (Exception e) {
            FineLoggerFactory.getLogger().info(e.getMessage());
            return;
        }
        res.setContentType("application/json;charset=utf-8");
        String result = json.toString();
        pw.println(result);
        pw.flush();
        pw.close();
    }

    public static void responseXml(HttpServletResponse res,String xml){
        PrintWriter pw;
        try {
            pw = WebUtils.createPrintWriter(res);
        } catch (Exception e) {
            FineLoggerFactory.getLogger().info(e.getMessage());
            return;
        }
        res.setContentType("text/xml;charset=utf-8");
        pw.println(xml);
        pw.flush();
        pw.close();
    }

    public static void setCSRFHeader(HttpServletResponse httpServletResponse){
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH");
        httpServletResponse.setHeader("Access-Control-Max-Age", "36000");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization,authorization");
    }

    public static void responseJsonp(HttpServletRequest req, HttpServletResponse res, JSONObject json){
        PrintWriter pw;
        try {
            pw = WebUtils.createPrintWriter(res);
        } catch (Exception e) {
            FineLoggerFactory.getLogger().info(e.getMessage());
            return;
        }
        res.setContentType("text/javascript;charset=utf-8;charset=utf-8");
        String result = json.toString();

        String jsonp=req.getParameter("callback");

        pw.println(jsonp+"("+result+")");
        pw.flush();
        pw.close();
    }

    public static Response fail(){
        return Response.error("500","操作失败");
    }

    public static Response fail(String msg){
        return Response.error("500",msg);
    }



    public static Response failQuery(){
        return Response.error("500","查询失败");
    }

    public static Response success(Object obj){
        Response response = new Response();
        response.setData(obj);
        response.setErrorCode("200");
        return response;
    }

}
